package com.example.maybeaccount.Mode;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/** 用于一些时间的运算 */
public class MyTimer {
    /** 获取某月天数 */
    public static int getDayOfMonth(int year,int month){
        Calendar c = Calendar.getInstance();
        c.set(year, month, 0); //输入类型为int类型
        return c.get(Calendar.DAY_OF_MONTH);
    }
    /** 把字符转为Calendar */
    public static Calendar getTimeByString(String stringTime){
        //将字符串转化为Date，添加至Calendar
        Date date = StringDateUtils.cutDate(stringTime);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    }

    public static String getCurrentStringTime(String pattern){
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(cal.getTime());
    }

    public static String long2StringTime(Long millis, String pattern){
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(millis);
        cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(cal.getTime());
    }
}
